package com.mentorgen.tools.profile.output;

import java.util.ArrayList;
import java.util.List;

import com.mentorgen.tools.profile.runtime.EntityDictionary;
import com.mentorgen.tools.profile.runtime.BundleInternals;
import com.mentorgen.tools.profile.runtime.ServiceInternals;

public class JipBundleHandler implements IJipBundleHandler {
	
	private boolean bundleStart = false;
	private boolean serviceStart = false;
	private BundleInternals bundle;
	private ServiceInternals service;

	private static List<String> mBundleServiceSkipList;
	private String bundleName;
	private String serviceName;
	
	public JipBundleHandler(){
		mBundleServiceSkipList = new ArrayList<String>();
	}

	@Override
	public void endBundle() throws Exception {
		if (!bundleStart)
			throw new Exception("Bundle is not started yet");
		bundleStart = false;
		bundle	   = null;
		bundleName = null;
	}

	@Override
	public void endService() throws Exception {
		if (!serviceStart)
			throw new Exception("Service is not started yet");
		
		serviceStart = false;
		service 	= null;
		serviceName = null;
	}

	@Override
	public void getBundle(String _bundleName) throws Exception {
		
		bundleName = _bundleName;
		
		if (inSkipList(bundleName))
			return;
		
		if (!bundleStart)
			throw new Exception("Bundle Element Not Started");
		
		bundle = EntityDictionary.getBundleDic().get(bundleName);
		if (bundle == null)
			throw new Exception ("Bundle Not Found For Name: " + bundleName);
	}

	@Override
	public void getService(String _serviceName) throws Exception{
		
		serviceName = _serviceName;
		
		if (inSkipList(serviceName) || inSkipList(bundleName))
			return;
		
		if (!serviceStart)
			throw new Exception ("Service Element Not Started");
		
		service = bundle.getService(serviceName);
		if(service == null){
			service = new ServiceInternals(serviceName);
			bundle.addServiec(service);
		}
	}

	@Override
	public void setAvgDataIn(String datain) {
		if (service != null)
			service.setInData(datain);
	}

	@Override
	public void setAvgDataOut(String dataout) {
		if (service != null)
			service.setOutData(dataout);
	}

	@Override
	public void setTotalData(String totaldata) {
		if (service != null)
			service.setTotalData(totaldata);
	}

	@Override
	public void startBundle() throws Exception{
		
		if (bundleStart || bundleName != null)
			throw new Exception ("The State is already in another bundle");
		bundleStart = true;
	}

	@Override
	public void startService() throws Exception {
		
		if (bundleStart && !serviceStart && serviceName == null)
			serviceStart = true;
		else 
			throw new Exception ("Improper Service Start");
	}

	@Override
	public void verifyService(String id) throws Exception {
		if (!bundleStart || !serviceStart)
			throw new Exception("Improper File Format. Bundle not started.");
		if (!inSkipList(serviceName) && !inSkipList(bundleName))
			service.setServiceID(id);
	}
	
	public void skipListAdd(String elem){
		mBundleServiceSkipList.add(elem);
	}
	
	public void skipListRemove(String elem){
		mBundleServiceSkipList.remove(elem);
	}
	
	private boolean inSkipList(String elem){
		if (mBundleServiceSkipList.contains(elem))
			return true;
		return false;
	}
}
